//
//  HSYBaseNavigationController+Animation.m
//  高仿暴走斗图
//
//  Created by key on 16/3/21.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseNavigationController+Animation.h"
#import "HSYBaseNavigationController+CardAnimation.h"
#import "HSYBaseNavigationController+MoveAnimation.h"

@implementation HSYBaseNavigationController (Animation)

#pragma mark - Move Calculation

- (void)moveViewWithX:(CGFloat)x withBlackMask:(UIView *)blackMask startBackViewX:(CGFloat)startBackViewX lastScreenShotView:(UIImageView *)lastScreenShotView {
    
    [self calculateOffsetForMoveX:x withBlackMask:blackMask startBackViewX:startBackViewX lastScreenShotView:lastScreenShotView TransitionsType:kHSYTransitionsAnimationTypeMove];
}

- (void)cardViewWithX:(CGFloat)x withBlackMask:(UIView *)blackMask startBackViewX:(CGFloat)startBackViewX lastScreenShotView:(UIImageView *)lastScreenShotView {
    
    lastScreenShotView.layer.shadowColor   = [UIColor blackColor].CGColor;
    lastScreenShotView.layer.shadowOpacity = 0.80f;
    lastScreenShotView.layer.shadowRadius  = 8.00f;
    
    [self calculateOffsetForMoveX:x withBlackMask:blackMask startBackViewX:startBackViewX lastScreenShotView:lastScreenShotView TransitionsType:kHSYTransitionsAnimationTypeCard];
}


- (void)calculateOffsetForMoveX:(CGFloat)x withBlackMask:(UIView *)blackMask startBackViewX:(CGFloat)startBackViewX lastScreenShotView:(UIImageView *)lastScreenShotView TransitionsType:(kHSYTransitionsAnimationType)type {
    
    if (! blackMask || !lastScreenShotView) {
        return;
    }
    
    x = x>= IPHONE_WIDTH ? IPHONE_WIDTH : x;
    x = x<= 0 ? 0 : x;
    
    CGRect frame = self.view.frame;
    frame.origin.x = x;
    self.view.frame = frame;
    
    switch (type) {
            
        case kHSYTransitionsAnimationTypeMove: {
            
            CGFloat alpha = 0.4 - (x/IPHONE_WIDTH);
            blackMask.alpha = alpha;
            
            CGFloat scale = fabs(startBackViewX)/IPHONE_WIDTH;
            CGFloat y = x *scale;
            CGFloat lastScreenShotViewHeight = IPHONE_HEIGHT;
            
            CGFloat statusH;
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_7_0
            
            statusH = 20.0f;
#else
            statusH = 0.0f;
#endif
            lastScreenShotViewHeight = lastScreenShotViewHeight -statusH;
            
            lastScreenShotView.frame = CGRectMake(startBackViewX +y, 0, IPHONE_WIDTH, lastScreenShotViewHeight);
        }
            break;
         
        case kHSYTransitionsAnimationTypeCard: {
            
            CGFloat alpha = (x/IPHONE_WIDTH);
            blackMask.alpha = alpha;
            
            CGFloat offset = CARD_ANIMATION_OFFSET - (CARD_ANIMATION_OFFSET *alpha);
            NSLog(@"offset: %f", offset);
            lastScreenShotView.frame = CGRectMake(offset, offset, IPHONE_WIDTH - offset*2, IPHONE_HEIGHT - offset*2);
        }
            break;
            
        case kHSYTransitionsAnimationTypeRotation: {
            
        }
            break;
            
            
        default:
            break;
    }

}


#pragma mark - Move Status

- (void)endStutasWithLocation:(CGFloat)location endAnimationType:(kHSYAnimationEndType)type {
    
    @weakify(self);
    switch (self.animationType) {
        case kHSYTransitionsAnimationTypeMove: {
            
            [self moveAnimationDidEndingForLocation:location withCompletion:^(BOOL finished) {
                
                switch (type) {
                    case kHSYAnimationEndTypeCancel: {
                        
                        @strongify(self);
                        [self cancelStatus];
                    }
                        break;
                    
                    case kHSYAnimationEndTypeEnd: {
                        
                        @strongify(self);
                        [self endStatus];
                    }
                        break;
                        
                    default:
                        break;
                }
            }];
        }
            break;
            
        case kHSYTransitionsAnimationTypeCard: {
            
            [self cardAnimationDidEndingForLocation:location withCompletion:^(BOOL finished) {
                switch (type) {
                    case kHSYAnimationEndTypeCancel: {
                        
                        @strongify(self);
                        [self cancelStatus];
                    }
                        break;
                        
                    case kHSYAnimationEndTypeEnd: {
                        
                        @strongify(self);
                        [self endStatus];
                    }
                        break;
                        
                    default:
                        break;
                }
            }];
        }
            break;
            
        case kHSYTransitionsAnimationTypeRotation: {
            
        }
            break;
            
        default:
            break;
    }
}

- (void)cancelStatus {
    
    self.backgroundView.hidden = YES;
}

- (void)endStatus {
    
    [self popViewControllerAnimated:NO];
    CGRect newFrame = [UIScreen getKeyWindows].bounds;
    self.lastScreenShotView.hidden = YES;
    newFrame.origin.x = 0;
    self.view.frame = newFrame;
}

#pragma mark - Move Animation

- (void)pop_MoveAnimationWithDuration:(NSTimeInterval)duration animations:(void(^)())animations completion:(void(^)(BOOL finished))completion {
    
    [UIView animateWithDuration:duration animations:animations completion:^(BOOL finished) {
        completion(finished);
    }];
}

- (void)pop_CardAnimationWithDuration:(NSTimeInterval)duration options:(UIViewAnimationOptions)op animations:(void(^)())animations completion:(void(^)(BOOL finished))completion {
    
    [UIView animateWithDuration:duration delay:0.0f options:op animations:animations completion:^(BOOL finished) {
        completion(finished);
    }];
}


@end
